package edu.mju.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.mju.eduservice.entity.StudyChapter;
import edu.mju.eduservice.entity.StudyVideo;
import edu.mju.eduservice.mapper.StudyChapterMapper;
import edu.mju.eduservice.service.StudyChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.mju.eduservice.service.StudyVideoService;
import edu.mju.eduservice.vo.chapter.ChapterVo;
import edu.mju.eduservice.vo.chapter.VideoVo;
import edu.mju.servicebase.exception.StuException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-31
 */
@Service
public class StudyChapterServiceImpl extends ServiceImpl<StudyChapterMapper, StudyChapter> implements StudyChapterService {

    @Autowired
    private StudyVideoService studyVideoService;

    /**
     * 根据ID查询课程大纲列表
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterByCourseId(String courseId) {
        //章节
        QueryWrapper<StudyChapter> wrapperChapter = new QueryWrapper<>();
        //小节
        QueryWrapper<StudyVideo> wrapperVideo = new QueryWrapper<>();

        //最终返回的数据
        List<ChapterVo> list = new ArrayList<>();

        //1. 根据课程id查询章节
        wrapperChapter.eq("course_id",courseId);
        List<StudyChapter> studyChapterList = baseMapper.selectList(wrapperChapter);

        //2. 查询小节
        wrapperVideo.eq("course_id",courseId);
        List<StudyVideo> studyVideoList = studyVideoService.list(wrapperVideo);

        //3. 遍历查询章节 封装
        for (int i = 0; i < studyChapterList.size(); i++) {
            //获取每个章节
            StudyChapter studyChapter = studyChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            //复制章节对象到chapterVo中去
            BeanUtils.copyProperties(studyChapter,chapterVo);

            //把chapterVo放入返回的集合中去
            list.add(chapterVo);

            //封装小节
            List<VideoVo> videoVoList = new ArrayList<>();

            //4. 遍历查询小节 封装
            for (int j = 0; j < studyVideoList.size(); j++) {
                //获取小节
                StudyVideo studyVideo = studyVideoList.get(j);
                //判断小节的chapterId是否和章节id一致
                if(studyChapter.getId().equals(studyVideo.getChapterId())){
                    //封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(studyVideo,videoVo);
                    videoVoList.add(videoVo);
                }

            }

            chapterVo.setChildren(videoVoList);


        }

        return list;
    }

    /**
     * 删除章节
     * 根据章节id查询是否有小节，有小节不删除！
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //查询小节表：有数据 无法删除
        QueryWrapper<StudyVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);

        int count = studyVideoService.count(wrapper);

        if(count>0){
            throw new StuException(20006,"deleteChapter无法删除");
        }else {
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }
    }

    /**
     * 删除所有章节
     * @param courseId 课程id
     */
    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<StudyChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
